home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 404_02 / bisnp / files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-16  |  12.1 KB  |  517 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #ifdef VMS
  22. #include <ssdef.h>
  23. #define unlink delete
  24. #ifndef XPFILE
  25. #define XPFILE "GNU_BISON:[000000]BISON.CC"
  26. #endif
  27. #ifndef XPFILE1
  28. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  29. #endif
  30. #ifndef XHFILE
  31. #define XHFILE "GNU_BISON:[000000]BISON.H"
  32. #endif
  33. #else
  34. #ifndef XPFILE
  35. #define XPFILE "bison.cc"
  36. #endif
  37. #ifndef XPFILE1
  38. #define XPFILE1 "bison.hairy"
  39. #endif
  40. #ifndef XHFILE
  41. #define XHFILE "bison.h"
  42. #endif
  43. #endif
  44.  
  45. #include <stdio.h>
  46. #include "system.h"
  47. #include "files.h"
  48. #include "new.h"
  49. #include "gram.h"
  50.  
  51. FILE *finput = NULL;
  52. FILE *foutput = NULL;
  53. FILE *fdefines = NULL;
  54. FILE *ftable = NULL;
  55. FILE *fattrs = NULL;
  56. FILE *fguard = NULL;
  57. FILE *faction = NULL;
  58. FILE *fparser = NULL;
  59.  
  60. /* File name specified with -o for the output file, or 0 if no -o.  */
  61. char *spec_outfile;
  62.  
  63. char *infile;
  64. char *outfile;
  65. char *defsfile;
  66. char *tabfile;
  67. char *attrsfile;
  68. char *guardfile;
  69. char *actfile;
  70. char *tmpattrsfile;
  71. char *tmptabfile;
  72. char *tmpdefsfile;
  73. /* AC added */
  74. char *hskelfile=NULL;
  75. char *cparserfile=NULL;
  76. FILE *fhskel=NULL;
  77. char *parser_name="parse"; 
  78. int parser_defined=0;
  79. int line_fparser=1;
  80. int line_fhskel=1;
  81. char *parser_fname="bison.cc";
  82. char *hskel_fname="bison.h";
  83. char *header_name=NULL;
  84. /* AC added end*/
  85.  
  86.  
  87.  
  88. extern char     *mktemp();      /* So the compiler won't complain */
  89. extern char     *getenv();
  90. extern void     perror();
  91. FILE    *tryopen();     /* This might be a good idea */
  92. void done();
  93.  
  94. extern char *program_name;
  95. extern int verboseflag;
  96. extern int definesflag;
  97. int fixed_outfiles = 0;
  98.  
  99. static char *c_suffixes[]=
  100.    {".tab.c",".tab.cc",".tab.cpp",".tab.cxx",".tab.C",
  101.     ".c",".cc",".cpp",".cxx",".C",".CPP",".CXX",".CC",(char *)0};
  102.  
  103.  
  104.  
  105. char*
  106. stringappend(string1, end1, string2)
  107. char *string1;
  108. int end1;
  109. char *string2;
  110. {
  111.   register char *ostring;
  112.   register char *cp, *cp1;
  113.   register int i;
  114.  
  115.   cp = string2;  i = 0;
  116.   while (*cp++) i++;
  117.  
  118.   ostring = NEW2(i+end1+1, char);
  119.  
  120.   cp = ostring;
  121.   cp1 = string1;
  122.   for (i = 0; i < end1; i++)
  123.     *cp++ = *cp1++;
  124.  
  125.   cp1 = string2;
  126.   while (*cp++ = *cp1++) ;
  127.  
  128.   return ostring;
  129. }
  130.  
  131.  
  132. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  133.    the output files, and opens the tmp files and the parser */
  134. void
  135. openfiles()
  136. {
  137.   char *name_base;
  138.   register char *cp;
  139.   char *filename;
  140.   int base_length;
  141.   int short_base_length;
  142.  
  143. #ifdef VMS
  144.   char *tmp_base = "sys$scratch:b_";
  145. #else
  146.   char *tmp_base = "/tmp/b.";
  147. #endif
  148.   int tmp_len;
  149.  
  150. #ifdef _MSDOS
  151.   tmp_base = "";
  152.   strlwr (infile);
  153. #endif /* MSDOS */
  154.  
  155.   tmp_len = strlen (tmp_base);
  156.  
  157.   if (spec_outfile)
  158.     {
  159.       /* -o was specified.  The precise -o name will be used for ftable.
  160.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  161.       name_base = spec_outfile;
  162. #ifdef _MSDOS
  163.       strlwr (name_base);
  164. #endif /* MSDOS */
  165.       base_length = strlen (name_base);
  166.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  167.        {char **suffix;
  168.     for(suffix=c_suffixes;*suffix;suffix++)
  169.     /* try to detect .c .cpp .tab.c ... options */
  170.       {if(strlen(name_base)>strlen(*suffix) 
  171.           && strcmp(name_base+base_length-strlen(*suffix),*suffix)==0)
  172.         { base_length -= strlen(*suffix);
  173.          break;}
  174.       };
  175.     }
  176.        short_base_length=base_length;
  177.     }
  178.   else if (spec_file_prefix)
  179.     {
  180.       /* -b was specified.  Construct names from it.  */
  181.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  182.       short_base_length = strlen (spec_file_prefix);
  183.       /* Count room for `.tab'.  */
  184.       base_length = short_base_length + 4;
  185.       name_base = (char *) xmalloc (base_length + 1);
  186.       /* Append `.tab'.  */
  187.       strcpy (name_base, spec_file_prefix);
  188. #ifdef VMS
  189.       strcat (name_base, "_tab");
  190. #else
  191.       strcat (name_base, ".tab");
  192. #endif
  193. #ifdef _MSDOS
  194.       strlwr (name_base);
  195. #endif /* MSDOS */
  196.     }
  197.   else
  198.     {
  199.       /* -o was not specified; compute output file name from input
  200.      or use y.tab.c, etc., if -y was specified.  */
  201.  
  202.       name_base = fixed_outfiles ? "y.y" : infile;
  203.  
  204.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  205.  
  206.       base_length = strlen (name_base);
  207.       if (!strcmp (name_base + base_length - 2, ".y"))
  208.     base_length -= 2;
  209.       short_base_length = base_length;
  210.  
  211. #ifdef VMS
  212.       name_base = stringappend(name_base, short_base_length, "_tab");
  213. #else
  214. #ifdef _MSDOS
  215.       name_base = stringappend(name_base, short_base_length, "_tab");
  216. #else
  217.       name_base = stringappend(name_base, short_base_length, ".tab");
  218. #endif /* not MSDOS */
  219. #endif
  220.       base_length = short_base_length + 4;
  221.     }
  222.  
  223.   finput = tryopen(infile, "r");
  224.  
  225.   filename=cparserfile;
  226.   if(filename==NULL)
  227.     filename = getenv("BISON_SIMPLE");
  228. #ifdef _MSDOS
  229.   /* File doesn't exist in current directory; try in INIT directory.  */
  230.   cp = getenv("INIT");
  231.   if (filename == 0 && cp != NULL)
  232.     {FILE *tst;
  233.       filename = (char *)xmalloc(strlen(cp) + strlen(PFILE) + 2);
  234.       strcpy(filename, PFILE);
  235.       if((tst=fopen(filename,"r"))!=NULL)
  236.        {fclose(tst);}
  237.       else 
  238.       {
  239.       strcpy(filename, cp);
  240.       cp = filename + strlen(filename);
  241.       *cp++ = '/';
  242.       strcpy(cp, PFILE);
  243.       }
  244.     }
  245. #endif /* MSDOS */
  246.   {char *p=filename ? filename : PFILE;
  247.    
  248.   parser_fname=(char *)xmalloc(strlen(p)+1);
  249.   strcpy(parser_fname,p);
  250.   }
  251.   fparser = tryopen(parser_fname, "r");
  252.  
  253.   filename=hskelfile;
  254.   if(filename==NULL)
  255.     filename = getenv("BISON_SIMPLE_H");
  256. #ifdef _MSDOS
  257.   /* File doesn't exist in current directory; try in INIT directory.  */
  258.   cp = getenv("INIT");
  259.   if (filename == 0 && cp != NULL)
  260.     {FILE *tst;
  261.       filename = (char *)xmalloc(strlen(cp) + strlen(HFILE) + 2);
  262.       strcpy(filename, HFILE);
  263.       if((tst=fopen(filename,"r"))!=NULL)
  264.        {fclose(tst);}
  265.       else 
  266.       {
  267.       strcpy(filename, cp);
  268.       cp = filename + strlen(filename);
  269.       *cp++ = '/';
  270.       strcpy(cp, HFILE);
  271.       }
  272.  
  273.     }
  274. #endif /* MSDOS */
  275.   {char *p=filename ? filename : HFILE;
  276.    
  277.   hskel_fname=(char *)xmalloc(strlen(p)+1);
  278.   strcpy(hskel_fname,p);
  279.   }
  280.  
  281.   fhskel = tryopen(hskel_fname, "r");
  282.  
  283.   if (verboseflag)
  284.     {
  285. #ifdef _MSDOS
  286.       outfile = stringappend(name_base, short_base_length, ".out");
  287. #else
  288.       if (spec_name_prefix)
  289.     outfile = stringappend(name_base, short_base_length, ".out");
  290.       else
  291.     outfile = stringappend(name_base, short_base_length, ".output");
  292. #endif
  293.       foutput = tryopen(outfile, "w");
  294.     }
  295.  
  296. #ifdef _MSDOS
  297.   actfile = _tempnam(".","b_ac");
  298.   tmpattrsfile = _tempnam(".","b_at");
  299.   tmptabfile = _tempnam(".","b_ta");
  300.   tmpdefsfile = _tempnam(".","b_de");
  301. #else
  302.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  303.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  304.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  305.   tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "defs.XXXXXX"));
  306. #endif /* not MSDOS */
  307.  
  308.   faction = tryopen(actfile, "w+");
  309.   fattrs = tryopen(tmpattrsfile,"w+");
  310.   ftable = tryopen(tmptabfile, "w+");
  311.  
  312.   if (definesflag)
  313.     { if(header_name)
  314.        defsfile=header_name;
  315.       else 
  316.        defsfile = stringappend(name_base, base_length, ".h");
  317.       fdefines = tryopen(tmpdefsfile, "w+");
  318.     }
  319.  
  320. #ifndef _MSDOS
  321.   unlink(actfile);
  322.   unlink(tmpattrsfile);
  323.   unlink(tmptabfile);
  324.   unlink(tmpdefsfile);
  325. #endif
  326.  
  327.     /* These are opened by `done' or `open_extra_files', if at all */
  328.   if (spec_outfile)
  329.     tabfile = spec_outfile;
  330.   else
  331.     tabfile = stringappend(name_base, base_length, ".c");
  332.  
  333. #ifdef VMS
  334.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  335.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  336. #else
  337. #ifdef _MSDOS
  338.   attrsfile = stringappend(name_base, short_base_length, ".sth");
  339.   guardfile = stringappend(name_base, short_base_length, ".guc");
  340. #else
  341.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  342.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  343. #endif /* not MSDOS */
  344. #endif /* not VMS */
  345. }
  346.  
  347.  
  348.  
  349. /* open the output files needed only for the semantic parser.
  350. This is done when %semantic_parser is seen in the declarations section.  */
  351.  
  352. void
  353. open_extra_files()
  354. {
  355.   FILE *ftmp;
  356.   int c;
  357.   char *filename, *cp;
  358.  
  359.   fclose(fparser);
  360.   filename=cparserfile;
  361.   if(filename!=NULL)
  362.     filename = (char *) getenv ("BISON_HAIRY");
  363. #ifdef _MSDOS
  364.   /* File doesn't exist in current directory; try in INIT directory.  */
  365.   cp = getenv("INIT");
  366.   if (filename == 0 && cp != NULL)
  367.     {FILE *tst;
  368.       filename = (char *)xmalloc(strlen(cp) + strlen(PFILE1) + 2);
  369.       strcpy(filename, PFILE1);
  370.       if((tst=fopen(filename,"r"))!=NULL)
  371.        {fclose(tst);}
  372.       else 
  373.       {
  374.       strcpy(filename, cp);
  375.       cp = filename + strlen(filename);
  376.       *cp++ = '/';
  377.       strcpy(cp, PFILE1);
  378.       }
  379.  
  380.     }
  381. #endif /* MSDOS */
  382.   {char *p=filename ? filename : PFILE1;
  383.    
  384.   parser_fname=(char *)xmalloc(strlen(p)+1);
  385.   strcpy(parser_fname,p);
  386.   }
  387.   fparser = tryopen(parser_fname, "r");
  388.  
  389.  
  390.         /* JF change from inline attrs file to separate one */
  391.   ftmp = tryopen(attrsfile, "w");
  392.   rewind(fattrs);
  393.   while((c=getc(fattrs))!=EOF)  /* Thank god for buffering */
  394.     putc(c,ftmp);
  395.   fclose(fattrs);
  396.   fattrs=ftmp;
  397.  
  398.   fguard = tryopen(guardfile, "w");
  399.  
  400. }
  401.  
  402.     /* JF to make file opening easier.  This func tries to open file
  403.        NAME with mode MODE, and prints an error message if it fails. */
  404. FILE *
  405. tryopen(name, mode)
  406. char *name;
  407. char *mode;
  408. {
  409.   FILE  *ptr;
  410.  
  411.   ptr = fopen(name, mode);
  412.   if (ptr == NULL)
  413.     {
  414.       fprintf(stderr, "%s: ", program_name);
  415.       perror(name);
  416.       done(2);
  417.     }
  418.   return ptr;
  419. }
  420.  
  421. void
  422. done(k)
  423. int k;
  424. {
  425.   if (faction)
  426.     fclose(faction);
  427.  
  428.   if (fattrs)
  429.     fclose(fattrs);
  430.  
  431.   if (fguard)
  432.     fclose(fguard);
  433.  
  434.   if (finput)
  435.     fclose(finput);
  436.  
  437.   if (fparser)
  438.     fclose(fparser);
  439.  
  440.   if (foutput)
  441.     fclose(foutput);
  442.  
  443.     /* JF write out the output file */
  444.   if (k == 0 && ftable)
  445.     {
  446.       FILE *ftmp;
  447.       register int c;
  448.       int lftmp=1;
  449.       char *pattern="\n#line @";
  450.       char *pospattern=pattern;
  451.       ftmp=tryopen(tabfile, "w");
  452. /* avoid reloading the definitions of tab.h */
  453.  
  454.       fprintf(ftmp,"#define YY_%s_h_included\n",parser_name);
  455.       lftmp++;
  456.       rewind(ftable);
  457.       while((c=getc(ftable)) != EOF)
  458.     {if(c=='\n') lftmp++;
  459.      if(c== *pospattern++ && c)
  460.           {if(*pospattern==0)
  461.             {fprintf(ftmp,"%d \"%s\"",lftmp+1, quoted_filename(tabfile));
  462.              pospattern=pattern;continue;}
  463.           }
  464.          else pospattern=pattern;
  465.          putc(c,ftmp);
  466.         }
  467.       fclose(ftmp);
  468.       fclose(ftable);
  469.  
  470.       if (definesflag)
  471.     { lftmp=1;
  472.       ftmp = tryopen(defsfile, "w");
  473.       fprintf(ftmp,"#ifndef YY_%s_h_included\n",parser_name);
  474.       fprintf(ftmp,"#define YY_%s_h_included\n",parser_name);
  475.           lftmp+=2;
  476.       fflush(fdefines);
  477.       rewind(fdefines);
  478.       while((c=getc(fdefines)) != EOF)
  479.         {if(c=='\n') lftmp++;
  480.          if(c==*pospattern++ && c)
  481.           {if(*pospattern==0)
  482.             {fprintf(ftmp,"%d \"%s\"",lftmp+1
  483.                                              ,quoted_filename(defsfile));
  484.                      pospattern=pattern;continue;}
  485.           }
  486.          else pospattern=pattern;
  487.          putc(c,ftmp);
  488.         }
  489.       fclose(fdefines);
  490.       fprintf(ftmp,"#endif\n");
  491.           lftmp++;
  492.       fclose(ftmp);
  493.     }
  494.     }
  495.  
  496. #ifdef VMS
  497.   if (faction)
  498.     delete(actfile);
  499.   if (fattrs)
  500.     delete(tmpattrsfile);
  501.   if (ftable)
  502.     delete(tmptabfile);
  503.   if (fdefines)
  504.     delete(tmpdefsfile);
  505.   if (k==0) sys$exit(SS$_NORMAL);
  506.   sys$exit(SS$_ABORT);
  507. #else
  508. #ifdef _MSDOS
  509.   if (actfile) unlink(actfile);
  510.   if (tmpattrsfile) unlink(tmpattrsfile);
  511.   if (tmptabfile) unlink(tmptabfile);
  512.   if (tmpdefsfile) unlink(tmpdefsfile);
  513. #endif /* MSDOS */
  514.   exit(k);
  515. #endif /* not VMS */
  516. }
  517.